/* xx5.7exp.p - Export PO with balance (from eb2.1 to 2015EE)
   Created By: Hayden (2015EE) 2016-06-14
   
   Purpose: Export po_mstr & pod_det & cmt_det from eb2.1 to *.txt
   Runtime: eb2.1
   
   Upgrade Step:
     1. run this program in eb2.1 generate *.txt files
     2. ftp *.txt files to 2015EE
     3. run xx5.7cim.p gen *.cim for each domain in 2015EE
     4. doing cimload with *.cim for each domain in 2015EE
*/

{mfdtitle.i}

def var DOMAIN_PRC as char init 'PRC'.
def var DOMAIN_HK  as char init 'HK'.

def temp-table tpo_mstr
  field tpo_nbr       like po_nbr
  field tpo_vend      like po_vend
  field tpo_ship      like po_ship
  field tpo_ord_date  like po_ord_date 
  field tpo_due_date  like po_due_date
  field tpo_buyer     like po_buyer
  field tpo_bill      like po_bill
  field tpo_contract  like po_contract
  field tpo_contact   like po_contact
  field tpo_rmks      like po_rmks
  field tpo_site      like po_site
  field tpo_project   like po_project
  field tpo_confirm   like po_confirm
  field tpo_curr      like po_curr
  field tpo_lang      like po_lang
  field tpo_taxable   like po_taxable
  field tpo_taxc      like po_taxc
  field tpo_tax_date  like po_tax_date
  field tpo_fix_pr    like po_fix_pr
  field tpo_cr_terms  like po_cr_terms
  field tpo_cmtindx   like po_cmtindx
  field tpo_tax_usage like po_tax_usage
  field tpo_tax_env   like po_tax_env  
  index itpo_nbr is primary tpo_nbr
  index itpo_cmtindx tpo_cmtindx.

def temp-table tcmt_det
  field tcmt_indx  like cmt_indx
  field tcmt_seq   like cmt_seq
  field tcmt_ref   like cmt_ref
  field tcmt_lang  like cmt_lang
  field tcmt_type  like cmt_type
  field tcmt_cmmt  like cmt_cmmt
  index itcmt_indx is primary tcmt_indx.

def temp-table tpod_det
  field tpod_nbr       like pod_nbr
  field tpod_line      like pod_line
  field tpod_site      like pod_site
  field tpod_req_nbr   like pod_req_nbr
  field tpod_part      like pod_part
  field tpod_qty_ord   like pod_qty_ord
  field tpod_um        like pod_um
  field tpod_pur_cost  like pod_pur_cost
  field tpod_disc_pct  like pod_disc_pct
  field tpod_lot_rcpt  like pod_lot_rcpt
  field tpod_loc       like pod_loc
  field tpod_rev       like pod_rev
  field tpod_status    like pod_status
  field tpod_vpart     like pod_vpart
  field tpod_due_date  like pod_due_date
  field tpod_per_date  like pod_per_date
  field tpod_need      like pod_need
  field tpod_so_job    like pod_so_job
  field tpod_fix_pr    like pod_fix_pr
  field tpod_acct      like pod_acct
  field tpod_sub       like pod_sub
  field tpod_cc        like pod_cc
  field tpod_project   like pod_project
  field tpod_type      like pod_type
  field tpod_taxable   like pod_taxable
  field tpod_taxc      like pod_taxc
  field tpod_insp_rqd  like pod_insp_rqd
  field tpod_um_conv   like pod_um_conv
  field tpod_cst_up    like pod_cst_up
  field tpod_tax_usage like pod_tax_usage
  field tpod_tax_env   like pod_tax_env
  index itpod_nbr_line is primary tpod_nbr tpod_line.
  
def temp-table tvp_mstr
  field tvp_vend like vp_vend
  index itvp_vend is primary tvp_vend.

function writepo_prc returns logical (input file as char):
  os-delete value(file).
  output to value(file).
  put "po_nbr|po_vend|po_ship|po_ord_date|"
      "po_due_date|po_buyer|po_bill|po_contract|"
      "po_contact|po_rmks|po_site|po_project|"
      "po_confirm|po_curr|po_lang|po_taxable|po_taxc|"
      "po_tax_date|po_fix_pr|po_cr_terms|"
      "po_cmtindx|po_tax_usage|po_tax_env|" skip.
  for each tpo_mstr where tpo_site <> "MDL" no-lock:
    put   tpo_nbr
      "|" tpo_vend
      "|" tpo_ship
      "|" tpo_ord_date
      "|" tpo_due_date
      "|" tpo_buyer
      "|" tpo_bill
      "|" tpo_contract
      "|" tpo_contact
      "|" tpo_rmks
      "|" tpo_site
      "|" tpo_project
      "|" tpo_confirm
      "|" tpo_curr
      "|" tpo_lang
      "|" tpo_taxable
      "|" tpo_taxc
      "|" tpo_tax_date
      "|" tpo_fix_pr
      "|" tpo_cr_terms
      "|" tpo_cmtindx
      "|" tpo_tax_usage
      "|" tpo_tax_env
      "|" skip.
  end.
  output close.
  return yes.
end.

function writecmt_prc return logical (input file as char):
  os-delete value(file).
  output to value(file).
  put "cmt_indx|cmt_seq|cmt_ref|cmt_lang|cmt_type|"
      "cmt_cmmt[1]|cmt_cmmt[2]|cmt_cmmt[3]|cmt_cmmt[4]|"
      "cmt_cmmt[5]|cmt_cmmt[6]|cmt_cmmt[7]|cmt_cmmt[8]|"
      "cmt_cmmt[9]|cmt_cmmt[10]|cmt_cmmt[11]|cmt_cmmt[12]|"
      "cmt_cmmt[13]|cmt_cmmt[14]|cmt_cmmt[15]|" skip.
  for each tcmt_det, each tpo_mstr where tcmt_indx = tpo_cmtindx
                                     and tpo_site <> "MDL"
                                     no-lock:
    put   tcmt_indx
      "|" tcmt_seq
      "|" tcmt_ref
      "|" tcmt_lang
      "|" tcmt_type
      "|" tcmt_cmmt[1]
      "|" tcmt_cmmt[2]
      "|" tcmt_cmmt[3]
      "|" tcmt_cmmt[4]
      "|" tcmt_cmmt[5]
      "|" tcmt_cmmt[6]
      "|" tcmt_cmmt[7]
      "|" tcmt_cmmt[8]
      "|" tcmt_cmmt[9]
      "|" tcmt_cmmt[10]
      "|" tcmt_cmmt[11]
      "|" tcmt_cmmt[12]
      "|" tcmt_cmmt[13]
      "|" tcmt_cmmt[14]
      "|" tcmt_cmmt[15]
      "|" skip.
  end.
  output close.
  return yes.
end.

function writepod_prc returns logical (input file as char):
  os-delete value(file).
  output to value(file).
  put "pod_nbr|pod_line|pod_site|pod_req_nbr|"
      "pod_part|pod_qty_ord|pod_um|pod_pur_cost|"
      "pod_disc_pct|pod_lot_rcpt|pod_loc|pod_rev|"
      "pod_status|pod_vpart|pod_due_date|pod_per_date|"
      "pod_need|pod_so_job|pod_fix_pr|pod_acct|"
      "pod_sub|pod_cc|pod_project|pod_type|"
      "pod_taxable|pod_taxc|pod_insp_rqd|"
      "pod_um_conv|pod_cst_up|pod_tax_usage|pod_tax_env|" skip.
  for each tpod_det where tpod_site <> "MDL" no-lock:
    put   tpod_nbr
      "|" tpod_line
      "|" tpod_site
      "|" tpod_req_nbr
      "|" tpod_part
      "|" tpod_qty_ord
      "|" tpod_um
      "|" tpod_pur_cost
      "|" decimal(tpod_disc_pct)
      "|" tpod_lot_rcpt
      "|" tpod_loc
      "|" tpod_rev
      "|" tpod_status
      "|" tpod_vpart
      "|" tpod_due_date
      "|" tpod_per_date
      "|" tpod_need
      "|" tpod_so_job
      "|" tpod_fix_pr
      "|" tpod_acct
      "|" tpod_sub
      "|" tpod_cc
      "|" tpod_project
      "|" tpod_type
      "|" tpod_taxable
      "|" tpod_taxc
      "|" tpod_insp_rqd
      "|" tpod_um_conv
      "|" tpod_cst_up
      "|" tpod_tax_usage
      "|" tpod_tax_env
      "|" skip.
  end.
  output close.
  return yes.
end.

function writepo_hk returns logical (input file as char):
  os-delete value(file).
  output to value(file).
  put "po_nbr|po_vend|po_ship|po_ord_date|"
      "po_due_date|po_buyer|po_bill|po_contract|"
      "po_contact|po_rmks|po_site|po_project|"
      "po_confirm|po_curr|po_lang|po_taxable|po_taxc|"
      "po_tax_date|po_fix_pr|po_cr_terms|"
      "po_cmtindx|po_tax_usage|po_tax_env|" skip.
  for each tpo_mstr where tpo_site = "MDL" no-lock:
    put   tpo_nbr
      "|" tpo_vend
      "|" tpo_ship
      "|" tpo_ord_date
      "|" tpo_due_date
      "|" tpo_buyer
      "|" tpo_bill
      "|" tpo_contract
      "|" tpo_contact
      "|" tpo_rmks
      "|" tpo_site
      "|" tpo_project
      "|" tpo_confirm
      "|" tpo_curr
      "|" tpo_lang
      "|" tpo_taxable
      "|" tpo_taxc
      "|" tpo_tax_date
      "|" tpo_fix_pr
      "|" tpo_cr_terms
      "|" tpo_cmtindx
      "|" tpo_tax_usage
      "|" tpo_tax_env
      "|" skip.
  end.
  output close.
  return yes.
end.

function writecmt_hk return logical (input file as char):
  os-delete value(file).
  output to value(file).
  put "cmt_indx|cmt_seq|cmt_ref|cmt_lang|cmt_type|"
      "cmt_cmmt[1]|cmt_cmmt[2]|cmt_cmmt[3]|cmt_cmmt[4]|"
      "cmt_cmmt[5]|cmt_cmmt[6]|cmt_cmmt[7]|cmt_cmmt[8]|"
      "cmt_cmmt[9]|cmt_cmmt[10]|cmt_cmmt[11]|cmt_cmmt[12]|"
      "cmt_cmmt[13]|cmt_cmmt[14]|cmt_cmmt[15]|" skip.
  for each tcmt_det, each tpo_mstr where tcmt_indx = tpo_cmtindx
                                     and tpo_site = "MDL"
                                     no-lock:
    put   tcmt_indx
      "|" tcmt_seq
      "|" tcmt_ref
      "|" tcmt_lang
      "|" tcmt_type
      "|" tcmt_cmmt[1]
      "|" tcmt_cmmt[2]
      "|" tcmt_cmmt[3]
      "|" tcmt_cmmt[4]
      "|" tcmt_cmmt[5]
      "|" tcmt_cmmt[6]
      "|" tcmt_cmmt[7]
      "|" tcmt_cmmt[8]
      "|" tcmt_cmmt[9]
      "|" tcmt_cmmt[10]
      "|" tcmt_cmmt[11]
      "|" tcmt_cmmt[12]
      "|" tcmt_cmmt[13]
      "|" tcmt_cmmt[14]
      "|" tcmt_cmmt[15]
      "|" skip.
  end.
  output close.
  return yes.
end.

function writepod_hk returns logical (input file as char):
  os-delete value(file).
  output to value(file).
  put "pod_nbr|pod_line|pod_site|pod_req_nbr|"
      "pod_part|pod_qty_ord|pod_um|pod_pur_cost|"
      "pod_disc_pct|pod_lot_rcpt|pod_loc|pod_rev|"
      "pod_status|pod_vpart|pod_due_date|pod_per_date|"
      "pod_need|pod_so_job|pod_fix_pr|pod_acct|"
      "pod_sub|pod_cc|pod_project|pod_type|"
      "pod_taxable|pod_taxc|pod_insp_rqd|"
      "pod_um_conv|pod_cst_up|pod_tax_usage|pod_tax_env|" skip.
  for each tpod_det where tpod_site = "MDL" no-lock:
    put   tpod_nbr
      "|" tpod_line
      "|" tpod_site
      "|" tpod_req_nbr
      "|" tpod_part
      "|" tpod_qty_ord
      "|" tpod_um
      "|" tpod_pur_cost
      "|" decimal(tpod_disc_pct)
      "|" tpod_lot_rcpt
      "|" tpod_loc
      "|" tpod_rev
      "|" tpod_status
      "|" tpod_vpart
      "|" tpod_due_date
      "|" tpod_per_date
      "|" tpod_need
      "|" tpod_so_job
      "|" tpod_fix_pr
      "|" tpod_acct
      "|" tpod_sub
      "|" tpod_cc
      "|" tpod_project
      "|" tpod_type
      "|" tpod_taxable
      "|" tpod_taxc
      "|" tpod_insp_rqd
      "|" tpod_um_conv
      "|" tpod_cst_up
      "|" tpod_tax_usage
      "|" tpod_tax_env
      "|" skip.
  end.
  output close.
  return yes.
end.

function ishasqty returns logical (input nbr as char):
  for each pod_det where pod_domain = global_domain
                     and pod_nbr = nbr
                     no-lock:
    if pod_status <> "C" and pod_qty_ord - pod_qty_rcvd > 0 then return yes.
  end.
  return no.
end.

function loadpo returns logical ():
  for each po_mstr where po_domain = global_domain
                     and po_stat <> "C"
                     no-lock:
    if ishasqty(po_nbr) then do:
      create tpo_mstr.
      assign tpo_nbr      = po_nbr
             tpo_vend     = po_vend
             tpo_ship     = po_ship
             tpo_ord_date = po_ord_date
             tpo_due_date = po_due_date
             tpo_buyer    = po_buyer
             tpo_bill     = po_bill
             tpo_contract = po_contract
             tpo_contact  = po_contact
             tpo_rmks     = po_rmks.
      
      /* convert site */
      if "MDL" = po_site then tpo_site = "QUO".
      else if "YSEL-OEM" = po_site then tpo_site = "MDL".
      else tpo_site = po_site.

      assign tpo_project = po_project
             tpo_confirm = po_confirm.

      /* convert curr */
      if "RMB" = po_curr then tpo_curr = "CNY".
      else tpo_curr = po_curr.
      
      assign tpo_lang      = po_lang
             tpo_taxable   = po_taxable
             tpo_taxc      = po_taxc
             tpo_tax_date  = po_tax_date
             tpo_fix_pr    = po_fix_pr
             tpo_cr_terms  = po_cr_terms
             tpo_cmtindx   = po_cmtindx
             tpo_tax_usage = po_tax_usage
             tpo_tax_env   = po_tax_env.
      release tpo_mstr.
    end.
  end.
  return yes.
end.

function loadcmt return logical ():
  for each cmt_det, each tpo_mstr where cmt_domain = global_domain
                                    and cmt_indx = tpo_cmtindx
                                    and cmt_indx > 0
                                    no-lock use-index itpo_cmtindx:
    create tcmt_det.
    assign tcmt_indx     = cmt_indx
           tcmt_seq      = cmt_seq
           tcmt_ref      = cmt_ref
           tcmt_lang     = cmt_lang
           tcmt_type     = cmt_type
           tcmt_cmmt[1]  = cmt_cmmt[1]
           tcmt_cmmt[2]  = cmt_cmmt[2]
           tcmt_cmmt[3]  = cmt_cmmt[3]
           tcmt_cmmt[4]  = cmt_cmmt[4]
           tcmt_cmmt[5]  = cmt_cmmt[5]
           tcmt_cmmt[6]  = cmt_cmmt[6]
           tcmt_cmmt[7]  = cmt_cmmt[7]
           tcmt_cmmt[8]  = cmt_cmmt[8]
           tcmt_cmmt[9]  = cmt_cmmt[9]
           tcmt_cmmt[10] = cmt_cmmt[10]
           tcmt_cmmt[11] = cmt_cmmt[11]
           tcmt_cmmt[12] = cmt_cmmt[12]
           tcmt_cmmt[13] = cmt_cmmt[13]
           tcmt_cmmt[14] = cmt_cmmt[14]
           tcmt_cmmt[15] = cmt_cmmt[15].
    release tcmt_det.
  end.
  return yes.  
end.

function loadpod returns logical ():
  for each pod_det, each tpo_mstr where pod_domain = global_domain
                                    and pod_nbr = tpo_nbr
                                    and pod_status <> "C"
                                    and pod_qty_ord - pod_qty_rcvd > 0
                                    no-lock:
      create tpod_det.
      assign tpod_nbr  = pod_nbr
             tpod_line = pod_line.
             
      /* convert site */
      if "MDL" = pod_site then tpod_site = "QUO".
      else if "YSEL-OEM" = pod_site then tpod_site = "MDL".
      else tpod_site = pod_site.
      
      assign tpod_req_nbr = pod_req_nbr
             tpod_part    = pod_part.
      
      /* balance qty */
      tpod_qty_ord = pod_qty_ord - pod_qty_rcvd.
      
      assign tpod_um       = pod_um
             tpod_pur_cost = pod_pur_cost
             tpod_disc_pct = pod_disc_pct
             tpod_lot_rcpt = pod_lot_rcpt
             tpod_loc      = pod_loc
             tpod_rev      = pod_rev
             tpod_status   = pod_status
             tpod_vpart    = trim(pod_vpart)
             tpod_due_date = pod_due_date
             tpod_per_date = pod_per_date
             tpod_need     = pod_need
             tpod_so_job   = pod_so_job
             tpod_fix_pr   = pod_fix_pr.

      /* new a/c = old a/c + "00" */
      tpod_acct = pod_acct + "00".
             
      assign tpod_sub       = pod_sub
             tpod_cc        = pod_cc
             tpod_project   = pod_project
             tpod_type      = pod_type
             tpod_taxable   = pod_taxable
             tpod_taxc      = pod_taxc
             tpod_insp_rqd  = pod_insp_rqd
             tpod_um_conv   = pod_um_conv
             tpod_cst_up    = pod_cst_up
             tpod_tax_usage = pod_tax_usage
             tpod_tax_env   = pod_tax_env.
      release tpod_det.
  end.
  return yes.
end.

function uniquevp returns logical ():
  for each tpo_mstr no-lock:
    find first tvp_mstr where tvp_vend = trim(tpo_vend) no-lock no-error.
    if not available(tvp_mstr) then do:
      create tvp_mstr.
      assign tvp_vend = trim(tpo_vend).
      release tvp_mstr.
    end.
  end.
  return yes.
end.

function writevp returns logical (input file as char):
  os-delete value(file).
  output to value(file).
  put 'vp_vend' skip.
  for each tvp_mstr no-lock:
    put unformatted tvp_vend skip.
  end.
  output close.
  return yes.
end.

/* --------------------- */
def var poname      as char format 'x(12)' init '5.7-po_mstr' label 'PO  File'.
def var pofile_prc  as char format 'x(20)'.
def var pofile_hk   as char format 'x(20)'.

def var cmtname     as char format 'x(12)' init '5.7-cmt_det' label 'CMT File'.
def var cmtfile_prc as char format 'x(20)'.
def var cmtfile_hk  as char format 'x(20)'.

def var podname     as char format 'x(12)' init '5.7-pod_det' label 'POD File'.
def var podfile_prc as char format 'x(20)'.
def var podfile_hk  as char format 'x(20)'.

def var vpname     as char format 'x(12)' init 'active-vps'  label 'VPS File'.
def var vpfile     as char format 'x(20)'.

pofile_prc  = DOMAIN_PRC + '-' + poname  + '.txt'.
pofile_hk   = DOMAIN_HK  + '-' + poname  + '.txt'.

cmtfile_prc = DOMAIN_PRC + '-' + cmtname + '.txt'.
cmtfile_hk  = DOMAIN_HK  + '-' + cmtname + '.txt'.

podfile_prc = DOMAIN_PRC + '-' + podname + '.txt'.
podfile_hk  = DOMAIN_HK  + '-' + podname + '.txt'.

vpfile      =                    vpname  + '.txt'.

repeat:
  disp 'Export po_mstr & pod_det & cmt_det from eb2.1 to *.txt' skip
       'Runtime: eb2.1' skip(1)
       'Upgrade Step:' skip
       '  1. run this program in eb2.1 generate *.txt files' skip
       '  2. ftp *.txt files to 2015EE' skip
       '  3. run xx5.7cim.p gen *.cim for each domain' skip
       '  4. doing cimload with *.cim for each domain' skip(1)
       poname  colon 14 '.txt' space(2) pofile_prc  no-label view-as text
                               space(2) pofile_hk   no-label view-as text skip
       cmtname colon 14 '.txt' space(2) cmtfile_prc no-label view-as text
                               space(2) cmtfile_hk  no-label view-as text skip
       podname colon 14 '.txt' space(2) podfile_prc no-label view-as text
                               space(2) podfile_hk  no-label view-as text skip
       vpname  colon 14 '.txt' space(2) vpfile      no-label view-as text skip
  with frame a width 80 side-label.
  
  prompt-for poname podname cmtname vpname with frame a editing:
    if frame-field = 'poname' then do:
      if input poname <> "" then do:
        disp DOMAIN_PRC + '-' + input poname + '.txt' @ pofile_prc with frame a.
        disp DOMAIN_HK  + '-' + input poname + '.txt' @ pofile_hk  with frame a.
      end.
      else do:
        disp '' @ pofile_prc with frame a.
        disp '' @ pofile_hk  with frame a.
      end.
    end.
    
    if frame-field = 'cmtname' then do:
      if input cmtname <> "" then do:
        disp DOMAIN_PRC + '-' + input cmtname + '.txt' @ cmtfile_prc with frame a.
        disp DOMAIN_HK  + '-' + input cmtname + '.txt' @ cmtfile_hk  with frame a.
      end.
      else do:
        disp '' @ cmtfile_prc with frame a.
        disp '' @ cmtfile_hk  with frame a.
      end.
    end.
    
    if frame-field = 'podname' then do:
      if input podname <> "" then do:
        disp DOMAIN_PRC + '-' + input podname + '.txt' @ podfile_prc with frame a.
        disp DOMAIN_HK  + '-' + input podname + '.txt' @ podfile_hk  with frame a.
      end.
      else do:
        disp '' @ podfile_prc with frame a.
        disp '' @ podfile_hk  with frame a.
      end.
    end.
    
    if frame-field = 'vpname' then do:
      if input vpname <> "" then do:
        disp input vpname + '.txt' @ vpfile with frame a.
      end.
      else do:
        disp '' @ vpfile with frame a.
      end.
    end.
    
    status input.
    readkey.
    apply lastkey.
  end.
  
  poname  = input poname .
  cmtname = input cmtname.
  podname = input podname.
  vpname  = input vpname.
  
  if poname = "" or cmtname = "" or podname = "" or vpname = "" then do:
    message 'Error: Please enter all filenames'.
    next.
  end.
  
  pofile_prc  = DOMAIN_PRC + '-' + poname  + '.txt'.
  pofile_hk   = DOMAIN_HK  + '-' + poname  + '.txt'.
  
  cmtfile_prc = DOMAIN_PRC + '-' + cmtname + '.txt'.
  cmtfile_hk  = DOMAIN_HK  + '-' + cmtname + '.txt'.
  
  podfile_prc = DOMAIN_PRC + '-' + podname + '.txt'.
  podfile_hk  = DOMAIN_HK  + '-' + podname + '.txt'.
  
  vpfile      =                    vpname  + '.txt'.
  
  /* load PO with balance to temp-table tpo_mstr */
  loadpo().
  writepo_prc(pofile_prc).
  writepo_hk (pofile_hk ).
  
  /* generate a list for all vend in PO */
  uniquevp().
  writevp(vpfile).
  
  /* load PO Comments to temp-table tcmt_det */
  loadcmt().
  writecmt_prc(cmtfile_prc).
  writecmt_hk (cmtfile_hk ).
  
  /* load PO Detail to temp-table tpod_det */
  loadpod().
  writepod_prc(podfile_prc).
  writepod_hk (podfile_hk ).

  message 'Completed!'.
end.
